#!/usr/bin/env python
#
#       Projectiles.py
#       
#       Copyright 2009 Pete Lord <mr.petelord@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#

import Options

from Client import Client
client = Client()

class Projectile():
    def __init__(self, xy, top, target, frames):
        self.x = xy[0]
        self.y = xy[1]
        self.target_x = self.target_location(target)[0]
        self.target_y = self.target_location(target)[1]
        self.top = top
        self.mod = 0
        self.amount = 1
        
        if self.x > self.target_x:
            self.x_speed = (self.x - self.target_x) / frames
        else:
            self.x_speed = -((self.target_x - self.x) / frames)
        if self.y > self.target_y:
            self.y_speed = (self.y - self.target_y) / frames
        else:
            self.y_speed = -((self.target_y - self.y) / frames)
    
    def poll(self):
        self.mod += self.amount
        if self.mod == self.top:
            self.amount = -self.amount
        self.x += self.x_speed
        self.y += self.y_speed
        
        
    def screen_location(self):
        # get the tuple (x, y) of the sprites screen location
        x, y = self.shadow_location()
        y += self.mod
        return (x, y - Options.TILE_HEIGHT[Options.ZOOM_LEVEL])
        
    def shadow_location(self):
        x = (self.x - self.y + client.game.map.height - 1) * 0.5 * Options.TILE_WIDTH[Options.ZOOM_LEVEL]
        y = (self.x + self.y) * 0.5 * Options.TILE_HEIGHT[Options.ZOOM_LEVEL]
        return (x, y)
        
    def target_location(self, target):
        x = (target[0] - target[1] + client.game.map.height - 1) * 0.5 * Options.TILE_WIDTH[Options.ZOOM_LEVEL]
        y = (target[0] + target[1]) * 0.5 * Options.TILE_HEIGHT[Options.ZOOM_LEVEL]
        return (x, y)